package com.accountbooks.justin;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.accountbooks.justin.DBHelper;

public class DBManager {
	
	DBHelper dbHelper;
	SQLiteDatabase db;
	
	public DBManager(Context context) {
		dbHelper = new DBHelper(context);
		db = dbHelper.getWritableDatabase();
	}
	
	public void closeDB() {
		db.close();
	}
	
	public void addAccountType (List<AccountType> accountTypeList) {
		db.beginTransaction();
		try {
			for (AccountType item : accountTypeList) {
				
				//ContentValues contentValues = new ContentValues(); 
				//contentValues.put("Account_Parent_Type_ID", item.getAccountParentTypeID());
				//contentValues.put("Account_Type_Name", item.getAccountTypeName());  
				//contentValues.put("Created_On", item.getCreatedOn().toLocaleString());  
				//contentValues.put("Modified_On", item.getModifiedOn().toLocaleString()); 
				//db.insert("AccountType", null, contentValues);
				
				db.execSQL("INSERT INTO Account_Type VALUES (null, ?, ?, ?, ?)", new Object[] { item.getParentTypeID(),
						item.getTypeName(),
						item.getCreatedOn(),
						item.getModifiedOn() });
			}
			db.setTransactionSuccessful(); 
		} finally {
			db.endTransaction();
		}
	}

	public void addAccountDetail(List<AccountDetail> accountDetailList) {
		db.beginTransaction();
		try {
			
			for (AccountDetail item : accountDetailList) {
				db.execSQL("INSERT INTO Account_Detail VALUES (null, ?, ?, ?, ?, ?)", new Object[] {item.getTypeID(),
						item.getSpendAmount(),
						item.getRemarkInfo(),
						item.getCreatedOn(),
						item.getModifiedOn()});
			}
			
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
	}

	public List<AccountType> getAllAccountType() {
		
		List<AccountType> accountTypeList = new ArrayList<AccountType>();
		Cursor cursor = db.rawQuery("SELECT * FROM Account_Type", null);
		
		while (cursor.moveToNext()) {
			
			AccountType item = new AccountType();
			
			item.setTypeID(cursor.getInt(cursor.getColumnIndex("Type_ID")));
			item.setParentTypeID(cursor.getInt(cursor.getColumnIndex("Parent_Type_ID")));
			item.setTypeName(cursor.getString(cursor.getColumnIndex("Type_Name")));
			item.setCreatedOn(cursor.getString(cursor.getColumnIndex("Created_On")));
			item.setModifiedOn(cursor.getString(cursor.getColumnIndex("Modified_On")));
			
			accountTypeList.add(item);
		}
		
		cursor.close();
		return accountTypeList;
	}
	
	public List<AccountType> getSubAccountTypeByID(int typeID) {
		List<AccountType> subAccountTypes = new ArrayList<AccountType>();
		
		return subAccountTypes;
	}
	
	public AccountType getAccountTypeByID(int typeID) {
		AccountType accountType = new AccountType();
		
		Cursor cursor = db.rawQuery("SELECT * FROM AccountType WHERE Type_ID = ?", new String[] { String.valueOf(typeID) });
		
		while (cursor.moveToNext()) {
			accountType.setTypeID(cursor.getInt(cursor.getColumnIndex("Type_ID")));
			accountType.setTypeName(cursor.getString(cursor.getColumnIndex("Type_Name")));
			accountType.setParentTypeID(cursor.getInt(cursor.getColumnIndex("Parent_Type_ID")));
			accountType.setCreatedOn(cursor.getString(cursor.getColumnIndex("Created_On")));
			accountType.setModifiedOn(cursor.getString(cursor.getColumnIndex("Modified_On")));
		}
		
		cursor.close();
		return accountType;
	}

	public List<AccountDetail> getAllAccountDetail() {
		
		List<AccountDetail> accountTypeList = new ArrayList<AccountDetail>();
		Cursor cursor = db.rawQuery("SELECT * FROM Account_Detail", null);
		
		while (cursor.moveToNext()) {
			
			AccountDetail item = new AccountDetail() {};
			
			item.setDetailID(cursor.getInt(cursor.getColumnIndex("Detail_ID")));
			item.setTypeID(cursor.getInt(cursor.getColumnIndex("Type_ID")));
			item.setSpendAmount(cursor.getFloat(cursor.getColumnIndex("Spend_Amount")));
			item.setRemarkInfo(cursor.getString(cursor.getColumnIndex("Remark_Info")));
			item.setCreatedOn(cursor.getString(cursor.getColumnIndex("Created_On")));
			item.setModifiedOn(cursor.getString(cursor.getColumnIndex("Modified_On")));
			
			accountTypeList.add(item);
		}
		
		cursor.close();
		return accountTypeList;
	}
	
	public AccountDetail getAccountDetailByID(int detailID) {
		AccountDetail accountDetail = new AccountDetail();
		
		return accountDetail;
	}
	
}
